//
//  YWPopupOrganizationViewController.m
//  maintain
//
//  Created by Kyo on 15/12/17.
//  Copyright © 2015年 ewin. All rights reserved.
//

#import "YWPopupOrganizationViewController.h"
#import "UIViewController+MJPopupViewController.h"
#import "YWMacro.h"
#import "UIColor+CustomColors.h"
#import "YWOrganization.h"
#import "UIImageView+WebCache.h"
#import "YWCommonTool.h"
#import "YWPopupOrganizationCell.h"

static CGFloat const leftWidth = 15.0f;
static CGFloat const viewHeight = 204.0f;
@interface YWPopupOrganizationViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView* tableView;
@end

@implementation YWPopupOrganizationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self.view setFrame:CGRectMake(leftWidth, (SCREEN_HEIGHT - viewHeight) / 2, SCREEN_WIDTH - leftWidth * 2, viewHeight)];
    
    [self setup];
}

#pragma mark private method
- (void)setup
{
    UILabel* headLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 10, CGRectGetWidth(self.view.frame) - 25, 22)];
    headLabel.textAlignment = NSTextAlignmentCenter;
    headLabel.text = NSLocalizedString(@"selectCompany", nil);
    [headLabel setFont:kBoldTitleFont];
    [self.view addSubview:headLabel];
    
    UIButton* xButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [xButton setImage:[UIImage imageNamed:@"btn_close_normal.png"] forState:UIControlStateNormal];
    [xButton setImage:[UIImage imageNamed:@"btn_close_selected.png"] forState:UIControlStateHighlighted];
    xButton.frame = CGRectMake(CGRectGetWidth(self.view.frame)-32-10, 5, 32, 32);
    [self.view addSubview:xButton];
    [xButton addTarget:self action:@selector(dismissSelf) forControlEvents:UIControlEventTouchUpInside];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(headLabel.frame)+5, self.view.frame.size.width, 162) style:UITableViewStylePlain];
    self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.tableView.rowHeight = 54;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:self.tableView];
    
}
-(void)dismissSelf{
    [self.parentVC dismissPopupViewControllerWithanimationType:MJPopupViewAnimationFade];
}

- (void)setOrganizations:(NSArray *)organizations{
    _organizations = organizations;
    [self.tableView reloadData];
}

#pragma mark UITableViewDataSource

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_organizations count];
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    YWPopupOrganizationCell *cell = [YWPopupOrganizationCell cellWithTableView:tableView];
    YWOrganization* organization = _organizations[indexPath.row];
    NSString* downloadUrl = [YWCommonTool pictureDownloadUrl:organization.organizationLogo.url];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:downloadUrl] placeholderImage:[UIImage imageNamed:@"profile_logo"]];
    cell.textLabel.text = organization.simpleName?:NSLocalizedString(@"none", nil);
    return cell;
}

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YWOrganizationNo* organization = _organizations[indexPath.row];
    if (_delegate != nil && [_delegate respondsToSelector:@selector(selectedOrganization:)]) {
        [_delegate selectedOrganization:organization];
    }
    
    [self.parentVC dismissPopupViewControllerWithanimationType:MJPopupViewAnimationFade];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
